package com.shark.demo;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName AddService
 * @Description TODO
 * @Author 东北彭于晏
 * @Date 2024/10/7  0:17
 * @Version 1.0
 **/
public class AddService {
    public static void main(String[] args) {
        NumberAndLetter1 numberAndLetter1 = new NumberAndLetter1();

        new Thread(() -> {
            numberAndLetter1.number();
        }).start();

        new Thread(() -> {
            numberAndLetter1.letter();
        }).start();

    }
}

class NumberAndLetter1 {
    Lock lock = new ReentrantLock();
    Condition numberCondition = lock.newCondition();
    Condition letterCondition = lock.newCondition();

    int count = 1;

    public void number() {
        lock.lock();
        try {
            for (int i = 1; i <= 52; i++) {
                while (count % 3 == 0) {
                    numberCondition.await();
                }
                System.out.print(i);
                count++;
                letterCondition.signal();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }

    }


    public void letter() {
        lock.lock();
        try {
            for (char i = 'A'; i <= 'Z'; i++) {
                while (count % 3 != 0) {
                    letterCondition.await();
                }
                System.out.print(i + "   ");
                count++;
                numberCondition.signal();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }


    }
}